package io.gps.datatransfer.utils;

import io.gps.datatransfer.CenterChannel;
import io.gps.datatransfer.command.DevServer;
import io.gps.datatransfer.command.TimeObj;
import org.apache.commons.lang3.StringUtils;

import java.io.IOException;
import java.net.InetSocketAddress;
import java.net.Socket;
import java.util.*;

public class Utils {

    /**
     * 初始化字符串 并去除空格
     * @param str
     * @return
     */
    public static String delSpace(Object str){
        return StringUtils.deleteWhitespace(StringUtils.defaultString(String.valueOf(str)));
    }

    /**
     * 初始化字符串 并去除空格并且不允许出现 字符串 null
     * @param str
     * @return
     */
    public static String delSpaceNotStrNull(Object str){
        String outStr=delSpace(str);
        if("null".equalsIgnoreCase(outStr)){
            return "";
        }
        return outStr;
    }

    public static int getHourOrMin(int isH){
        Calendar calendar = Calendar.getInstance();
        if(isH==1){
            return calendar.get(Calendar.HOUR_OF_DAY);
        }else{
            return calendar.get(Calendar.MINUTE);
        }

    }


    public static boolean checkTime(){
        return SysDef.IS_TIME;
    }


    public static void taskCheckTime(){
        int h=getHourOrMin(1);
        int m=getHourOrMin(0);
        taskCheckTime(h ,m);
    }

    /**
     * 校验当前时间是否在规定的时间戳范围
     * @param h
     * @param m
     */
    public static void taskCheckTime(int h ,int m){
        boolean t=true;
        if(SysDef.IS_SET_HOUR){
            t=false;
            for (int i = 0; i < SysDef.time_List.size() ; i++) {
                TimeObj tempObj=SysDef.time_List.get(i);

                if((tempObj.getStartHour()>tempObj.getEndHour()&&(h>=tempObj.getStartHour()||h<=tempObj.getEndHour()))
                        ||(tempObj.getStartHour()<tempObj.getEndHour()&&h>=tempObj.getStartHour()&&h<=tempObj.getEndHour())){
                    if(h==tempObj.getStartHour()||h==tempObj.getEndHour()){
                        if(h==tempObj.getStartHour()&&m>=tempObj.getStartMin()){
                            t=true;
                            i=SysDef.time_List.size()+1;
                        }else if(h==tempObj.getEndHour()&&m<=tempObj.getEndMin()){
                            t=true;
                            i=SysDef.time_List.size()+1;
                        }
                    }else{
                       t=true;
                       i=SysDef.time_List.size()+1;
                    }
                }
            }
        }
        SysDef.IS_TIME=t;

    }


    public static void taskCheckTime(Long s){
        boolean t=true;
        int h=getHourOrMin(1,s);
        int m=getHourOrMin(0,s);
        taskCheckTime(h ,m);
    }

    public static int getHourOrMin(int isH,Long s){
        Calendar calendar = Calendar.getInstance();
        calendar.setTimeInMillis(s);
        if(isH==1){
            return calendar.get(Calendar.HOUR_OF_DAY);
        }else{
            return calendar.get(Calendar.MINUTE);
        }

    }

    public static void setLink(String sim){
        if(SysDef.IS_SET_ONE){
            sim=SysDef.IS_SET_ONE_SIMKEY;
        }
        for (int i = 0; i < SysDef.UP_SERVER.size(); i++) {
            CenterChannel center = SysDef.sim_server_center.get( SysDef.getserverkey(sim,SysDef.UP_SERVER.get(i).getServerid()));
            if(center==null||center.devServer.getServerid()==null){
                CenterChannel newCenter=new CenterChannel(SysDef.UP_SERVER.get(i),sim);
                SysDef.sim_server_center.put( SysDef.getserverkey(sim,SysDef.UP_SERVER.get(i).getServerid()),newCenter);
            }
        }
    }

    public static void setErrorLink(String serverid,String sim){
        if(SysDef.IS_SET_ONE){
            sim=SysDef.IS_SET_ONE_SIMKEY;
        }
        CenterChannel center = SysDef.sim_server_center.get(SysDef.getserverkey(sim,serverid));
        if(center!=null&&center.getDevServer().getServerid()!=null){
            center.disconnect("setErrorLink  强行回收");
        }
    }


    public static List<DevServer> getDevServer(String UP_SERVER_ADDRESS_STR){
        List<DevServer> tempDevserver=new ArrayList<DevServer>();
        String[] tempServers=UP_SERVER_ADDRESS_STR.split(",");
        if(tempServers!=null&&tempServers.length>0){
            for (int i = 0; i <tempServers.length ; i++) {
                String[] tempServer=tempServers[i].split(":");
                DevServer tempSer=new DevServer(tempServer[0],Integer.parseInt(tempServer[1]),Integer.parseInt(tempServer[2]));
                tempDevserver.add(tempSer);
            }
        }
       return tempDevserver;
    }

    public static boolean isHostConnectable(String host, int port) {
        Socket socket = new Socket();
        try {
            socket.connect(new InetSocketAddress(host, port));
        } catch (IOException e) {
            e.printStackTrace();
            return false;
        } finally {
            try {
                socket.close();
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
        return true;
    }

    /**
     * 字符编码格式转换
     * @param instr 需要转换的字符
     * @param incharsetName  需要转换的字符的编码格式
     * @param outcharsetName 需要获得的字符的编码格式
     * @return
     */
    public static String changeCharset(String instr,String incharsetName,String outcharsetName){
        try {
            String incharsetstr=new String(instr.getBytes(incharsetName),incharsetName);
            String outcharsetstr=new String(incharsetstr.getBytes(outcharsetName),outcharsetName);
            return outcharsetstr;
        } catch (Exception e) {
            e.printStackTrace();
            // TODO: handle exception
        }
        return instr;
    }


}
